home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: Split Filename
- Date: Thu, 01 Feb 1996 11:49:40 +0200
- Organization: Carelcomp Forest
- Message-ID: <31108CB4.59ED@cmt.lpr.mail.carel.fi>
- References: <4e6lsr$8ar@news1.radix.net>,<31075FB4.3E53@cmt.lpr.mail.carel.fi> <DM23s4.77J@news.cern.ch>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Maurizio Loreti wrote:
- >
- > In article <31075FB4.3E53@cmt.lpr.mail.carel.fi>, Ari Lukumies <aril@cmt.lpr.mail.carel.fi> writes:
- > >Jim Ward wrote:
- > >>
- > >> Is there a C function that will split
- > >> /home/file.c into /home and file.c ?
- > >>
- > >> Thanks,
- > >>
- > >> Jim Ward
- > >
- > >You could try:
- > >
- > > char pathandfile[] = "/home/file.c";
- > > char *path = pathandfile;
- > > char *p = strrchr(pathandfile, '/');
- > > char *filename = p + 1;
- > > *p = '\0';
- > >
- > >Also, some systems have functions named makepath and splitpath.
- > >
- > >Later,
- > >AriL
- > >
- > >
- > >--
- > >All my opinions are mine and mine alone.
- >
- > Try with pathhandfile[] = "file.c" ... core dump.
-
- The original poster was asking to split "/home/file.c", _not_ "file.c". In real life,
- you should of course test the outcomes (and actually, char pathandfile[] = "xxx";
- doesn't work with some compilers if this is done inside a function...):
-
- char pathandfile[] = "/home/file.c";
- char *path;
- char *p;
- char *filename;
- if (path = pathandfile) {
- if (p = strrchr(pathandfile, '/')) {
- path = pathandfile;
- filename = p + 1;
- *p = '\0';
- }
- else {
- path = NULL;
- filename = pathandfile;
- }
- }
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-